﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi_Gui.LayoutManager;
using Yugi.Client.ViewModel;
using Yugi.Client;
using Yugi.Client.Control;
using Yugi.Client.WebSerive;
namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for LoginLayout.xaml
	/// </summary>
	public partial class DeckConstructLayout : UserControl , IInvoke
	{
        MainServerHandler mainserver;
        DeckConstructModel model;
        WebServiceCommunication cm = new WebServiceCommunication();

        public DeckConstructLayout()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(LoginLayout_Loaded);
		}

        void LoginLayout_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                mainserver = ((Window)(this.Parent)).DataContext as MainServerHandler;
                model = mainserver.Deck_Model;
                model.Presenter = this;
                cm.SetAuth("yugioh", "admin123");
                LoadDeck();
            }
            catch (Exception ex)
            {
                
            }
            
           
        }


        private void Save_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            List<int> list = new List<int>(this.deck.GetCurrentDeck());
            this.mainserver.SendMessageToServer(Common.MSG_GSM.L_Update_Card, list);
            msg_box.ShowWait();
        }

        private void Back_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            LayoutManager.LayoutManager.Insitance.ChangeToLayout(LayoutType.Room);
        }

        #region IInvoke Members

        public void Do(ClientMSG msg, params object[] obj)
        {
            switch(msg)
            {
                case ClientMSG.UpdateCard_OK:
                    msg_box.CloseWait();
                    ((Canvas)this.msg_box.Parent).IsHitTestVisible = true;
                    break;
                case ClientMSG.UpdateCard_Error:
                    msg_box.CloseWait();
                    ((Canvas)this.msg_box.Parent).IsHitTestVisible = true;
                    break;
            }

        }
        public void Refresh()
        {
            
        }
       

        #endregion

        public Image GetCardImageDetail()
        {
            return Card_Image;
        }
        public YCardDetailText GetCardTextDetail()
        {
            return Card_Detail;
        }
        public Deck_Container GetDeckContainer()
        {
            return deck;
        }

        private void LoadDeck()
        {
            try
            {
                PlayerInfo p = cm.GetAllCard(mainserver.External_Model.UserID);
                this.store.InitializeCard(p.Storage,p.Deck);
                
            }
            catch(Exception ex)
            {
                MessageBox.Show("Lỗi Load Cards");
            }
        }
    }
}